/********************************************************************************/
/*										*/
/*		Algorithm Values from the TCG Algorithm Registry 		*/
/*			     Written by Ken Goldman				*/
/*		       IBM Thomas J. Watson Research Center			*/
/*										*/
/*  Licenses and Notices							*/
/*										*/
/*  1. Copyright Licenses:							*/
/*										*/
/*  - Trusted Computing Group (TCG) grants to the user of the source code in	*/
/*    this specification (the "Source Code") a worldwide, irrevocable, 		*/
/*    nonexclusive, royalty free, copyright license to reproduce, create 	*/
/*    derivative works, distribute, display and perform the Source Code and	*/
/*    derivative works thereof, and to grant others the rights granted herein.	*/
/*										*/
/*  - The TCG grants to the user of the other parts of the specification 	*/
/*    (other than the Source Code) the rights to reproduce, distribute, 	*/
/*    display, and perform the specification solely for the purpose of 		*/
/*    developing products based on such documents.				*/
/*										*/
/*  2. Source Code Distribution Conditions:					*/
/*										*/
/*  - Redistributions of Source Code must retain the above copyright licenses, 	*/
/*    this list of conditions and the following disclaimers.			*/
/*										*/
/*  - Redistributions in binary form must reproduce the above copyright 	*/
/*    licenses, this list of conditions	and the following disclaimers in the 	*/
/*    documentation and/or other materials provided with the distribution.	*/
/*										*/
/*  3. Disclaimers:								*/
/*										*/
/*  - THE COPYRIGHT LICENSES SET FORTH ABOVE DO NOT REPRESENT ANY FORM OF	*/
/*  LICENSE OR WAIVER, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, WITH	*/
/*  RESPECT TO PATENT RIGHTS HELD BY TCG MEMBERS (OR OTHER THIRD PARTIES)	*/
/*  THAT MAY BE NECESSARY TO IMPLEMENT THIS SPECIFICATION OR OTHERWISE.		*/
/*  Contact TCG Administration (admin@trustedcomputinggroup.org) for 		*/
/*  information on specification licensing rights available through TCG 	*/
/*  membership agreements.							*/
/*										*/
/*  - THIS SPECIFICATION IS PROVIDED "AS IS" WITH NO EXPRESS OR IMPLIED 	*/
/*    WARRANTIES WHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY OR 	*/
/*    FITNESS FOR A PARTICULAR PURPOSE, ACCURACY, COMPLETENESS, OR 		*/
/*    NONINFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS, OR ANY WARRANTY 		*/
/*    OTHERWISE ARISING OUT OF ANY PROPOSAL, SPECIFICATION OR SAMPLE.		*/
/*										*/
/*  - Without limitation, TCG and its members and licensors disclaim all 	*/
/*    liability, including liability for infringement of any proprietary 	*/
/*    rights, relating to use of information in this specification and to the	*/
/*    implementation of this specification, and TCG disclaims all liability for	*/
/*    cost of procurement of substitute goods or services, lost profits, loss 	*/
/*    of use, loss of data or any incidental, consequential, direct, indirect, 	*/
/*    or special damages, whether under contract, tort, warranty or otherwise, 	*/
/*    arising in any way out of use or reliance upon this specification or any 	*/
/*    information herein.							*/
/*										*/
/*  (c) Copyright IBM Corp. and others, 2019 - 2024				*/
/*										*/
/********************************************************************************/


// FILE GENERATED BY TpmExtractCode: DO NOT EDIT

#ifndef _TPM_INCLUDE_PRIVATE_TPMALGORITHMDEFINES_H_
#define _TPM_INCLUDE_PRIVATE_TPMALGORITHMDEFINES_H_

#include "TpmProfile.h"
#include "MinMax.h"
#include "TPMB.h"

#if ALG_ECC
// Table "Defines for NIST_P192 ECC Values" (TCG Algorithm Registry)
#  define NIST_P192_ID       TPM_ECC_NIST_P192
#  define NIST_P192_KEY_SIZE 192

// Table "Defines for NIST_P224 ECC Values" (TCG Algorithm Registry)
#  define NIST_P224_ID       TPM_ECC_NIST_P224
#  define NIST_P224_KEY_SIZE 224

// Table "Defines for NIST_P256 ECC Values" (TCG Algorithm Registry)
#  define NIST_P256_ID       TPM_ECC_NIST_P256
#  define NIST_P256_KEY_SIZE 256

// Table "Defines for NIST_P384 ECC Values" (TCG Algorithm Registry)
#  define NIST_P384_ID       TPM_ECC_NIST_P384
#  define NIST_P384_KEY_SIZE 384

// Table "Defines for NIST_P521 ECC Values" (TCG Algorithm Registry)
#  define NIST_P521_ID       TPM_ECC_NIST_P521
#  define NIST_P521_KEY_SIZE 521

// Table "Defines for BN_P256 ECC Values" (TCG Algorithm Registry)
#  define BN_P256_ID       TPM_ECC_BN_P256
#  define BN_P256_KEY_SIZE 256

// Table "Defines for BN_P638 ECC Values" (TCG Algorithm Registry)
#  define BN_P638_ID       TPM_ECC_BN_P638
#  define BN_P638_KEY_SIZE 638

// Table "Defines for SM2_P256 ECC Values" (TCG Algorithm Registry)
#  define SM2_P256_ID       TPM_ECC_SM2_P256
#  define SM2_P256_KEY_SIZE 256

// Table "Defines for BP_P256_R1 ECC Values" (TCG Algorithm Registry)
#  define BP_P256_R1_ID       TPM_ECC_BP_P256_R1
#  define BP_P256_R1_KEY_SIZE 256

// Table "Defines for BP_P384_R1 ECC Values" (TCG Algorithm Registry)
#  define BP_P384_R1_ID       TPM_ECC_BP_P384_R1
#  define BP_P384_R1_KEY_SIZE 384

// Table "Defines for BP_P512_R1 ECC Values" (TCG Algorithm Registry)
#  define BP_P512_R1_ID       TPM_ECC_BP_P512_R1
#  define BP_P512_R1_KEY_SIZE 512

// Table "Defines for CURVE_25519 ECC Values" (TCG Algorithm Registry)
#  define CURVE_25519_ID       TPM_ECC_CURVE_25519
#  define CURVE_25519_KEY_SIZE 256

// Table "Defines for CURVE_448 ECC Values" (TCG Algorithm Registry)
#  define CURVE_448_ID       TPM_ECC_CURVE_448
#  define CURVE_448_KEY_SIZE 448

// Derived ECC Value
#  define ECC_CURVES                                                       \
      {                                                                    \
          TPM_ECC_NIST_P192, TPM_ECC_NIST_P224, TPM_ECC_NIST_P256,         \
              TPM_ECC_NIST_P384, TPM_ECC_NIST_P521, TPM_ECC_BN_P256,       \
              TPM_ECC_BN_P638, TPM_ECC_SM2_P256, TPM_ECC_BP_P256_R1,       \
              TPM_ECC_BP_P384_R1, TPM_ECC_BP_P512_R1, TPM_ECC_CURVE_25519, \
              TPM_ECC_CURVE_448                                            \
      }

#  define ECC_CURVE_COUNT                                                            \
      (ECC_NIST_P192 + ECC_NIST_P224 + ECC_NIST_P256 + ECC_NIST_P384 + ECC_NIST_P521 \
       + ECC_BN_P256 + ECC_BN_P638 + ECC_SM2_P256 + ECC_BP_P256_R1 + ECC_BP_P384_R1  \
       + ECC_BP_P512_R1 + ECC_CURVE_25519 + ECC_CURVE_448)

// Avoid expanding MAX_ECC_KEY_BITS into a long expression, the compiler slows down
// and on some compilers runs out of heap space.

// 638
#  if ECC_BN_P638
#    define MAX_ECC_KEY_BITS BN_P638_KEY_SIZE
// 521
#  elif ECC_NIST_P521
#    define MAX_ECC_KEY_BITS NIST_P521_KEY_SIZE
#  elif ECC_BP_P512_R1
#    define MAX_ECC_KEY_BITS BP_P512_R1_KEY_SIZE
// 448
#  elif ECC_CURVE_448
#    define MAX_ECC_KEY_BITS CURVE_448_KEY_SIZE
// 384
#  elif ECC_NIST_P384
#    define MAX_ECC_KEY_BITS NIST_P384_KEY_SIZE
#  elif ECC_BP_P384_R1
#    define MAX_ECC_KEY_BITS BP_P384_R1_KEY_SIZE
// 256
#  elif ECC_NIST_P256
#    define MAX_ECC_KEY_BITS NIST_P256_KEY_SIZE
#  elif TPM_ECC_BN_P256
#    define MAX_ECC_KEY_BITS BN_P256_KEY_SIZE
#  elif TPM_ECC_SM2_P256
#    define MAX_ECC_KEY_BITS SM2_P256_KEY_SIZE
#  elif TPM_ECC_CURVE_25519
#    define MAX_ECC_KEY_BITS CURVE_25519_KEY_SIZE
#  elif TPM_ECC_BP_P256_R1
#    define MAX_ECC_KEY_BITS BP_P256_R1_KEY_SIZE
// 224
#  elif ECC_NIST_P224
#    define MAX_ECC_KEY_BITS NIST_P224_KEY_SIZE
// 192
#  elif ECC_NIST_P192
#    define MAX_ECC_KEY_BITS NIST_P192_KEY_SIZE
#  else
#    error ALG_ECC enabled, but no ECC Curves Enabled
#  endif

#  define MAX_ECC_KEY_BYTES ((MAX_ECC_KEY_BITS + 7) / 8)

#endif  // ALG_ECC

#if ALG_RSA
// Table "Defines for RSA Asymmetric Cipher Algorithm Constants" (TCG Algorithm Registry)
#  define RSA_KEY_SIZES_BITS                                                      \
      (RSA_1024 * 1024), (RSA_2048 * 2048), (RSA_3072 * 3072), (RSA_4096 * 4096), \
          (RSA_16384 * 16384)

#  if RSA_16384
#    define RSA_MAX_KEY_SIZE_BITS 16384
#  elif RSA_4096
#    define RSA_MAX_KEY_SIZE_BITS 4096
#  elif RSA_3072
#    define RSA_MAX_KEY_SIZE_BITS 3072
#  elif RSA_2048
#    define RSA_MAX_KEY_SIZE_BITS 2048
#  elif RSA_1024
#    define RSA_MAX_KEY_SIZE_BITS 1024
#  else
#    error RSA Enabled, but no RSA key sizes enabled.
#  endif

#  define MAX_RSA_KEY_BITS  RSA_MAX_KEY_SIZE_BITS
#  define MAX_RSA_KEY_BYTES BITS_TO_BYTES(RSA_MAX_KEY_SIZE_BITS)
#endif  // ALG_RSA

// Table "Defines for AES Symmetric Cipher Algorithm Constants" (TCG Algorithm Registry)
#define AES_KEY_SIZES_BITS (AES_128 * 128), (AES_192 * 192), (AES_256 * 256)
#define AES_MAX_KEY_SIZE_BITS \
    MAX((AES_256 * 256), MAX((AES_192 * 192), (AES_128 * 128)))
#define MAX_AES_KEY_BITS  AES_MAX_KEY_SIZE_BITS
#define MAX_AES_KEY_BYTES BITS_TO_BYTES(MAX_AES_KEY_BITS)
#define AES_BLOCK_SIZES   (AES_128 * 128 / 8), (AES_192 * 128 / 8), (AES_256 * 128 / 8)
#define MAX_AES_BLOCK_SIZE_BYTES \
    MAX((AES_256 * 128 / 8), MAX((AES_192 * 128 / 8), (AES_128 * 128 / 8)))
#define AES_MAX_BLOCK_SIZE MAX_AES_BLOCK_SIZE_BYTES

// Table "Defines for SM4 Symmetric Cipher Algorithm Constants" (TCG Algorithm Registry)
#define SM4_KEY_SIZES_BITS       (SM4_128 * 128)
#define SM4_MAX_KEY_SIZE_BITS    (SM4_128 * 128)
#define MAX_SM4_KEY_BITS         SM4_MAX_KEY_SIZE_BITS
#define MAX_SM4_KEY_BYTES        BITS_TO_BYTES(MAX_SM4_KEY_BITS)
#define SM4_BLOCK_SIZES          (SM4_128 * 128 / 8)
#define MAX_SM4_BLOCK_SIZE_BYTES (SM4_128 * 128 / 8)
#define SM4_MAX_BLOCK_SIZE       MAX_SM4_BLOCK_SIZE_BYTES

// Table "Defines for CAMELLIA Symmetric Cipher Algorithm Constants" (TCG Algorithm Registry)
#define CAMELLIA_KEY_SIZES_BITS \
    (CAMELLIA_128 * 128), (CAMELLIA_192 * 192), (CAMELLIA_256 * 256)
#define CAMELLIA_MAX_KEY_SIZE_BITS \
    MAX((CAMELLIA_256 * 256), MAX((CAMELLIA_192 * 192), (CAMELLIA_128 * 128)))
#define MAX_CAMELLIA_KEY_BITS  CAMELLIA_MAX_KEY_SIZE_BITS
#define MAX_CAMELLIA_KEY_BYTES BITS_TO_BYTES(MAX_CAMELLIA_KEY_BITS)
#define CAMELLIA_BLOCK_SIZES \
    (CAMELLIA_128 * 128 / 8), (CAMELLIA_192 * 128 / 8), (CAMELLIA_256 * 128 / 8)
#define MAX_CAMELLIA_BLOCK_SIZE_BYTES \
    MAX((CAMELLIA_256 * 128 / 8),     \
        MAX((CAMELLIA_192 * 128 / 8), (CAMELLIA_128 * 128 / 8)))
#define CAMELLIA_MAX_BLOCK_SIZE MAX_CAMELLIA_BLOCK_SIZE_BYTES

//										libtpms added begin
#define TDES_KEY_SIZES_BITS         (TDES_128 * 128), (TDES_192 * 192)
#define TDES_MAX_KEY_SIZE_BITS \
    MAX((TDES_192 * 192), (TDES_128 * 128))
#define MAX_TDES_KEY_BITS  TDES_MAX_KEY_SIZE_BITS
#define MAX_TDES_KEY_BYTES BITS_TO_BYTES(MAX_TDES_KEY_BITS)
#define TDES_BLOCK_SIZES   (TDES_128 * 8), (TDES_192 * 8)
#define MAX_TDES_BLOCK_SIZE_BYTES MAX((TDES_192 * 8), (TDES_128 * 8))
#define TDES_MAX_BLOCK_SIZE MAX_TDES_BLOCK_SIZE_BYTES
//										libtpms added end

// Derived Symmetric Values
#define SYM_COUNT ALG_AES + ALG_SM4 + ALG_CAMELLIA + ALG_TDES			// libtpms changed begin
#define MAX_SYM_BLOCK_SIZE \
    MAX(TDES_MAX_BLOCK_SIZE, \
    MAX(CAMELLIA_MAX_BLOCK_SIZE, MAX(SM4_MAX_BLOCK_SIZE, AES_MAX_BLOCK_SIZE)))
#define MAX_SYM_KEY_BITS \
    MAX(CAMELLIA_MAX_KEY_SIZE_BITS, MAX(SM4_MAX_KEY_SIZE_BITS, AES_MAX_KEY_SIZE_BITS))
#define MAX_SYM_KEY_BYTES ((MAX_SYM_KEY_BITS + 7) / 8)
#if MAX_SYM_KEY_BITS == 0 || MAX_SYM_BLOCK_SIZE == 0
#   error Bad size for MAX_SYM_KEY_BITS or MAX_SYM_BLOCK
#endif										// libtpms changed end

// Table "Defines for SHA1 Hash Values" (TCG Algorithm Registry)
#define SHA1_DIGEST_SIZE 20
#define SHA1_BLOCK_SIZE  64

// Table "Defines for SHA256 Hash Values" (TCG Algorithm Registry)
#define SHA256_DIGEST_SIZE 32
#define SHA256_BLOCK_SIZE  64

// Table "Defines for SHA384 Hash Values" (TCG Algorithm Registry)
#define SHA384_DIGEST_SIZE 48
#define SHA384_BLOCK_SIZE  128

// Table "Defines for SHA512 Hash Values" (TCG Algorithm Registry)
#define SHA512_DIGEST_SIZE 64
#define SHA512_BLOCK_SIZE  128

// Table "Defines for SM3_256 Hash Values" (TCG Algorithm Registry)
#define SM3_256_DIGEST_SIZE 32
#define SM3_256_BLOCK_SIZE  64

// Table "Defines for SHA3_256 Hash Values" (TCG Algorithm Registry)
#define SHA3_256_DIGEST_SIZE 32
#define SHA3_256_BLOCK_SIZE  136

// Table "Defines for SHA3_384 Hash Values" (TCG Algorithm Registry)
#define SHA3_384_DIGEST_SIZE 48
#define SHA3_384_BLOCK_SIZE  104

// Table "Defines for SHA3_512 Hash Values" (TCG Algorithm Registry)
#define SHA3_512_DIGEST_SIZE 64
#define SHA3_512_BLOCK_SIZE  72

// Derived Hash Values
#define HASH_COUNT                                                                \
    (ALG_SHA1 + ALG_SHA256 + ALG_SHA384 + ALG_SHA512 + ALG_SM3_256 + ALG_SHA3_256 \
     + ALG_SHA3_384 + ALG_SHA3_512)

// Leaving these as MAX-based calculations because (a) they don't slow down the
// build noticably, and (b) hash block and digest sizes vary, so the #if
// cascades for these are significantly more error prone to maintain.
#define MAX_HASH_BLOCK_SIZE                                       \
    MAX((ALG_SHA3_512 * SHA3_512_BLOCK_SIZE),                     \
        MAX((ALG_SHA3_384 * SHA3_384_BLOCK_SIZE),                 \
            MAX((ALG_SHA3_256 * SHA3_256_BLOCK_SIZE),             \
                MAX((ALG_SM3_256 * SM3_256_BLOCK_SIZE),           \
                    MAX((ALG_SHA512 * SHA512_BLOCK_SIZE),         \
                        MAX((ALG_SHA384 * SHA384_BLOCK_SIZE),     \
                            MAX((ALG_SHA256 * SHA256_BLOCK_SIZE), \
                                (ALG_SHA1 * SHA1_BLOCK_SIZE))))))))

#define MAX_HASH_DIGEST_SIZE                                       \
    MAX((ALG_SHA3_512 * SHA3_512_DIGEST_SIZE),                     \
        MAX((ALG_SHA3_384 * SHA3_384_DIGEST_SIZE),                 \
            MAX((ALG_SHA3_256 * SHA3_256_DIGEST_SIZE),             \
                MAX((ALG_SM3_256 * SM3_256_DIGEST_SIZE),           \
                    MAX((ALG_SHA512 * SHA512_DIGEST_SIZE),         \
                        MAX((ALG_SHA384 * SHA384_DIGEST_SIZE),     \
                            MAX((ALG_SHA256 * SHA256_DIGEST_SIZE), \
                                (ALG_SHA1 * SHA1_DIGEST_SIZE))))))))

#define MAX_DIGEST_SIZE MAX_HASH_DIGEST_SIZE

#if MAX_HASH_DIGEST_SIZE == 0 || MAX_HASH_BLOCK_SIZE == 0
#  error "Hash data not valid"
#endif

// Define the 2B structure that would hold any hash block
TPM2B_TYPE(MAX_HASH_BLOCK, MAX_HASH_BLOCK_SIZE);

// Following typedef is for some old code
typedef TPM2B_MAX_HASH_BLOCK TPM2B_HASH_BLOCK;

#endif  // _TPM_INCLUDE_PRIVATE_TPMALGORITHMDEFINES_H_
